<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="券码" prop="ticketNum">
        <el-input
          v-model="queryParams.ticketNum"
          placeholder="请输入券码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="券码状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择券码状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员手机" prop="memberMobile">
        <el-input
          v-model="queryParams.memberMobile"
          placeholder="请输入会员手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['act:temp-hngh202306oscar:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
          v-hasPermi="['act:temp-hngh202306oscar:import']"
        >导入</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="ticketList" >
      <el-table-column label="券码" align="center" prop="ticketNum" />
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat" />
      <el-table-column label="订单号" align="center" prop="orderNum" />
      <el-table-column label="会员手机号" align="center" prop="memberMobile" />
      <el-table-column label="发放时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="系统备注" align="center" prop="sysRemark" />
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['act:ticket:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['act:ticket:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动-河南省直工会202306-送奥斯卡电影票对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if= "open" width="500px">
      <el-upload
        class="upload-demo"
        drag
        :action="action"
        :headers="headers"
        :limit = "limit"
        :on-success="handleUploadSuccess"
        :before-upload="beforeUpload"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="open = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTicket, getTicket, delTicket, addTicket, updateTicket, exportTicket } from "@/api/act/temp/hngh202306oscar/ticket";
import {mapGetters} from "vuex";

export default {
  name: "Ticket",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 活动-河南省直工会202306-送奥斯卡电影票表格数据
      ticketList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态  1 未发放   2 已发放字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ticketNum: undefined,
        state: undefined,
        store: 'OSCAR',
        orderNum: undefined,
        memberMobile: undefined,
        applyTime: undefined,
        sysRemark: undefined,
        gmtTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      action: process.env.VUE_APP_BASE_API + '/act/temp/hngh202306oscar/import',
      limit: 1
    };
  },
  computed: {
    ...mapGetters(['token']),
    headers() {
      return{
        "Authorization": this.token // 直接从本地获取token就行
      }
    }
  },
  created() {
    this.getList();
    this.getDicts("​hngh_oscar_ticket_state").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询活动-河南省直工会202306-送奥斯卡电影票列表 */
    getList() {
      this.loading = true;
      listTicket(this.queryParams).then(response => {
        this.ticketList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态  1 未发放   2 已发放字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        ticketNum: undefined,
        state: undefined,
        orderNum: undefined,
        memberMobile: undefined,
        applyTime: undefined,
        remark: undefined,
        sysRemark: undefined,
        gmtTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动-河南省直工会202306-送奥斯卡电影票";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTicket(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动-河南省直工会202306-送奥斯卡电影票";
      });
    },
    handleImport(){
        this.open = true;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTicket(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addTicket(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除活动-河南省直工会202306-送奥斯卡电影票编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTicket(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有活动-河南省直工会202306-送奥斯卡电影票数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTicket(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleUploadSuccess(res, file) {
      this.msgSuccess("导入操作已完成");
      this.getList()
    },
    beforeUpload(file) {
      let extension = file.name.substring(file.name.lastIndexOf('.')+1);
      // 判断文件类型
      let isType = extension == 'xlsx'  || extension == 'xls';
      if(!isType) {
        this.$message.warning('只能上传后缀是.xlsx或者.xls的文件');
      }
      const isLt = file.size / 1024 / 1024 < 4;
      if (!isLt) {
        this.$message.error('上传文件大小不能超过M!');
      }
      return isType && isLt;
    },
  }
};
</script>
